package com.googlecode.jawb.tests.customtypes;

import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

import org.junit.Test;

import com.googlecode.jawb.BinaryType;
import com.googlecode.jawb.JAWB;
import com.googlecode.jawb.clues.SetType;

public class CustomTypes {
	@SetType(type = BinaryType.OBJECT, staticLengthType = CustomInteger.class)
	private int value;
	
	@SetType(type = BinaryType.OBJECT, dynamicLengthType = CustomString.class)
	private String string;
	
	@Test()
	public void test() throws IOException {
		CustomTypes result = (CustomTypes) new JAWB().loadFrom(
			new ByteArrayInputStream(
				new byte[]{
					0x01, 0x02, 0x03, 0x04, // value
					
					0x41, 0x42, 0x43, 0x00, // string
				}
			),
			CustomTypes.class
		);
		
		assertEquals(result.value, 10);
		assertEquals(result.string, "ABC");
		
		result.value	= 0xAABBCCDD;
		result.string 	= "CBA";
		
		ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
		new JAWB().saveTo(outputStream, result);
		
		assertArrayEquals(
			new byte[]{
				(byte) 0xAA, (byte) 0xBB, (byte) 0xCC, (byte) 0xDD, // value
				(byte) 0x43, (byte) 0x42, (byte) 0x41, (byte) 0x00, // string
			},
			outputStream.toByteArray()
		);
	}
}
